<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tintuc_model extends CI_Model {

	var $matt='';
	var $tieude='';
	var $tomtat='';
	var $noidung='';
	var $hinh='';
	var $ngaydang='';
	var $tacgia='';
	var $tieude_en='';
	var $tomtat_en='';
	var $noidung_en='';
	var $ma_tk='';
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }
	 
	function insert($data)
    {
        $this->db->insert('tintuc', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];
    }
	function xoahinhtintuc($matt)
	{
		$row=$this->db->query("SELECT `hinh` FROM `tintuc` WHERE `matt`='".$matt."'");
		$db1=$row->row_array();
		
		if(!empty($db1))
		{
			$myFile = $db1['hinh'];
			$myFile=str_replace("/", "\\", $myFile);
			$unlinkdir = getcwd().'\\'.$myFile; 
			unlink($unlinkdir);
			
		}
	}
	
	
	function delete($matt)
    {
		$this->xoahinhtintuc($matt);
			
		$result=$this->db->query("SELECT `tenhinh` FROM `hinh` WHERE `ma_tt`='".$matt."'");
		$db2=$result->result_array();
		
		if(!empty($db2))
		{
			foreach ($db2 as $h)
			{
				if(!empty($h))
				{
					$myFile = $h['tenhinh'];
					$myFile=str_replace("/", "\\", $myFile);
					$unlinkdir = getcwd().'\\'.$myFile;
					unlink($unlinkdir);
					
				}
			}
			
		}
			
		$this->db->delete('hinh', "`ma_tt` = '".$matt."'");
        $this->db->delete('tintuc', "`matt` = '".$matt."'");
	
    }
	function gettintuc($matt)
	{
		$this->db->select("matt,tieude,tomtat,noidung,hinh,ngaydang,tacgia,tieude_en,tomtat_en,noidung_en");
		$this->db->where("matt",$matt);
        $query=$this->db->get("tintuc");
       	return $query->row_array();
	}
	function insert_hinh($data)
    {
      	return $this->db->insert('hinh', $data);
    }
	function getresult_hinh($matt)
    {
      	$this->db->select("mah,tenhinh");
		$this->db->where("ma_tt",$matt);
        $query=$this->db->get("hinh");
       	return $query->result_array();
    }
	function update($data,$matt)
    {
		$this->db->where("matt",$matt);
      	return $this->db->update('tintuc', $data);
    }
	function getresult_index()
	{
		$data= $this->input->get();
		$index=1;
		$baitren_mottrang=15;
		
		if(!empty($data['page']))
		{
			$index=$data['page'];
		}else $data['page']=1;
		$query1=null;
		$query2=null;
		if(!empty($data['key']))
		{	
			$query1=$this->db->query("SELECT `matt` FROM `tintuc` WHERE  `tieude` like '%".$data['key']."%' or `tomtat` like '%".$data['key']."%'  or `tieude_en` like '%".$data['key']."%' or `tomtat_en` like '%".$data['key']."%'") ;
			$query2=$this->db->query("SELECT `matt`,`tieude`,`tomtat`,`noidung`,`hinh`,DATE_FORMAT(ngaydang,'%d/%m/%y %h:%i:%s') as ngaydang,`tacgia`,`tieude_en`,`tomtat_en`,`noidung_en`,`tentaikhoan` FROM `tintuc` left join `taikhoan` on tintuc.ma_tk = taikhoan.matk  WHERE  `tieude` like '%".$data['key']."%' or `tomtat` like '%".$data['key']."%'  or `tieude_en` like '%".$data['key']."%' or `tomtat_en` like '%".$data['key']."%' ORDER BY `ngaydang` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}
		else 
		{
			$query1=$this->db->query("SELECT `matt` FROM `tintuc` left join `taikhoan` on tintuc.ma_tk = taikhoan.matk ") ;
			$query2=$this->db->query("SELECT `matt`,`tieude`,`tomtat`,`noidung`,`hinh`,DATE_FORMAT(ngaydang,'%d/%m/%y %h:%i:%s') as ngaydang,`tacgia`,`tieude_en`,`tomtat_en`,`noidung_en`,`tentaikhoan` FROM `tintuc` left join `taikhoan` on tintuc.ma_tk = taikhoan.matk  ORDER BY `ngaydang` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}	
		
		$db1=$query1->num_rows();
		$db2=$query2->result_array();
        
		$data['pages']=$db1/$baitren_mottrang;
		$data['tintucs']=$db2;
		$data['pagesize']=$baitren_mottrang;
       	return $data;
	 }
}

?>